using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ECommerce.Library.Web;
using AutoGear.BLL;
using ECommerce.Library;

public partial class myaccount_edit_profile : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            if (SessionManager.IsUserLoggedIn)
            {
                SetPageTitleAndBreadcramb();
                uccBillingState.DataBind();
                uccBillingCountry.DataBind();
                uccShippingState.DataBind();
                uccShippingCountry.DataBind();
                LoadProfile();
            }
            else
            {
                Response.Redirect(string.IsNullOrEmpty(SupplierId) ? CustomSiteMap.Login : "~/partner/" + SupplierId + "/login.aspx");
            }

           
        }
    }


    private void SetPageTitleAndBreadcramb()
    {
        ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("cphMiddleContent");

        Literal litPageTitle = (Literal)ph.FindControl("litPageTitle");
        Literal litCurrentPage = (Literal)ph.FindControl("litCurrentPage");

        if (litPageTitle != null)
        {
            litCurrentPage.Text = litPageTitle.Text = "Edit Profile";

        }
    }

    protected void LoadProfile()
    {
        try
        {
            SiteUser siteUser = SessionManager.LoggedUser;
            if (siteUser is CustomerUser)
            {
                CustomerUser customerUser = siteUser as CustomerUser;
                customerUser = CustomerUser.GetCustomerUserByUserId(customerUser.UserId);
                tbxPersonalFirstName.Text = customerUser.Profile.FirstName;
                tbxPersonalLastName.Text = customerUser.Profile.LastName;
                tbxPersonalDateOfBirth.Text = ECommerce.Library.ECommerce.Utility.FormatDateTime(customerUser.Profile.DateOfBirth);
                tbxPersonalEmailAddress.Text = customerUser.Profile.EmailAddress;
                rbtMale.Checked = customerUser.Profile.UserGender == Gender.Male ? true : false;
                rbtFemale.Checked = customerUser.Profile.UserGender == Gender.Female ? true : false;
                tbxContactPhoneNumber.Text = customerUser.Profile.UserTelephoneNumber;
                tbxContactFaxNumber.Text = customerUser.Profile.UserFaxNumber;
                tbxCompanyName.Text = customerUser.Profile.CompanyName;
                tbxCompanyPhoneNumber.Text = customerUser.Profile.CompanyTelephoneNumber;
                tbxCompanyFaxNumber.Text = customerUser.Profile.CompanyFaxNumber;
                tbxBillingFirstName.Text = customerUser.Profile.BillingAddress.FirstName;
                tbxBillingLastName.Text = customerUser.Profile.BillingAddress.LastName;
                tbxBillingPhoneNumber.Text = customerUser.Profile.BillingAddress.TelephoneNumber;
                tbxBillingEmailAddress.Text = customerUser.Profile.BillingAddress.EmailAddress;
                tbxBillingAddressLine1.Text = customerUser.Profile.BillingAddress.AddressLine1;
                tbxBillingAddressLine2.Text = customerUser.Profile.BillingAddress.AddressLine2;
                tbxBillingCity.Text = customerUser.Profile.BillingAddress.City;
                uccBillingState.Items.FindByText(customerUser.Profile.BillingAddress.AddressState).Selected = true;
                uccBillingCountry.Items.FindByText(customerUser.Profile.BillingAddress.AddressCountry).Selected = true;
                tbxBillingZipCode.Text = customerUser.Profile.BillingAddress.ZipCode;
                tbxShippingFirstName.Text = customerUser.Profile.ShippingAddress.FirstName;
                tbxShippingLastName.Text = customerUser.Profile.ShippingAddress.LastName;
                tbxShippingPhoneNumber.Text = customerUser.Profile.ShippingAddress.TelephoneNumber;
                tbxShippingEmailAddress.Text = customerUser.Profile.ShippingAddress.EmailAddress;
                tbxShippingAddressLine1.Text = customerUser.Profile.ShippingAddress.AddressLine1;
                tbxShippingAddressLine2.Text = customerUser.Profile.ShippingAddress.AddressLine2;
                tbxShippingCity.Text = customerUser.Profile.ShippingAddress.City;
                uccShippingState.Items.FindByText(customerUser.Profile.ShippingAddress.AddressState).Selected = true;
                uccShippingCountry.Items.FindByText(customerUser.Profile.ShippingAddress.AddressCountry).Selected = true;
                tbxShippingZipCode.Text = customerUser.Profile.ShippingAddress.ZipCode;
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to load your account information.");
            uccResponseMessage.Visible = true;
        }
    }

    protected void imbModify_Click(object sender, EventArgs e)
    {
        try
        {
           // this.Page.Validate();

            if (IsValid)
            {
                SiteUser siteUser = SessionManager.LoggedUser;
                CustomerUser customerUser = CustomerUser.GetCustomerUserByUserName(siteUser.LoginUserName);
                if (customerUser != null)
                {
                    customerUser.Profile.FirstName = Security.BindStringInput(Security.BindStringInput(tbxPersonalFirstName.Text, SecurityLevel.Two), SecurityLevel.Three);
                    customerUser.Profile.LastName = Security.BindStringInput(Security.BindStringInput(tbxPersonalLastName.Text, SecurityLevel.Two), SecurityLevel.Three);
                    DateTime dateOfBirth;
                    if (!DateTime.TryParse(tbxPersonalDateOfBirth.Text, out dateOfBirth))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect date.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.DateOfBirth = dateOfBirth;
                    string emailAddress = tbxPersonalEmailAddress.Text.Trim();
                    if (!RegExUtil.IsEmail(emailAddress))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Personal Email.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.EmailAddress = emailAddress;
                    Gender userGender;
                    if (rbtMale.Checked)
                    {
                        userGender = Gender.Male;
                    }
                    else
                    {
                        userGender = Gender.Female;
                    }

                    customerUser.Profile.UserGender = userGender;
                    string userTelephoneNumber = tbxContactPhoneNumber.Text.Trim();
                    if (!RegExUtil.IsPhoneNumber(userTelephoneNumber))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Personal Phone Number.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.UserTelephoneNumber = userTelephoneNumber;

                    string userFaxNumber = tbxContactFaxNumber.Text.Trim();
                    if (!string.IsNullOrEmpty(userFaxNumber) && !RegExUtil.IsPhoneNumber(userFaxNumber))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Personal Fax Number.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.UserFaxNumber = userFaxNumber;
                    customerUser.Profile.CompanyName = Security.BindStringInput(tbxCompanyName.Text, SecurityLevel.Three);
                    string companyTelephoneNumber = tbxCompanyPhoneNumber.Text.Trim();
                    if (!RegExUtil.IsPhoneNumber(companyTelephoneNumber))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Company Telephone Number.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.CompanyTelephoneNumber = companyTelephoneNumber;

                    string companyFaxNumber = tbxCompanyFaxNumber.Text.Trim();
                    if (!string.IsNullOrEmpty(companyFaxNumber) && !RegExUtil.IsPhoneNumber(companyFaxNumber))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect Company Fax Number.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.CompanyFaxNumber = companyFaxNumber;
                    customerUser.Profile.BillingAddress = new AutoGear.BLL.Address(tbxBillingFirstName.Text, tbxBillingLastName.Text, tbxBillingAddressLine1.Text, tbxBillingAddressLine2.Text, tbxBillingCity.Text, uccBillingState.SelectedItem.Text, uccBillingCountry.SelectedItem.Text, tbxBillingZipCode.Text, tbxBillingPhoneNumber.Text, string.Empty, tbxBillingEmailAddress.Text);
                    customerUser.Profile.ShippingAddress = new AutoGear.BLL.Address(tbxShippingFirstName.Text, tbxShippingLastName.Text, tbxShippingAddressLine1.Text, tbxShippingAddressLine2.Text, tbxShippingCity.Text, uccShippingState.SelectedItem.Text, uccShippingCountry.SelectedItem.Text, tbxShippingZipCode.Text, tbxShippingPhoneNumber.Text, string.Empty, tbxShippingEmailAddress.Text);
                    customerUser.Profile.IsActive = true;
                    customerUser.Update();

                    if (customerUser != null)
                    {
                        AutoGearSiteUser.Login(customerUser.LoginUserName, customerUser.LoginPassword, UserLocationType.FrontEnd);
                        uccResponseMessage.SetSuccessMessage("Your account information updated.");
                        uccResponseMessage.Visible = true;
                        //master_private masterPrivate = this.Master as master_private;
                        //masterPrivate.LoadSiteUser();
                    }
                    else
                    {
                        throw new Exception();
                    }
                }

            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to modify your account information.");
            uccResponseMessage.Visible = true;
        }
    }
}
